package commands;

import logic.TaskDirector;
import logic.UndoRedoManager;
import storage.Task;
import storage.UndoRedoDetails;

public class Undo {

	UndoRedoManager manager = new UndoRedoManager();
	AvailableCommands command_;

	// create instance of each action
	Add add = new Add();
	Remove remove = new Remove();
	Edit edit = new Edit();
	Complete complete = new Complete();
	Search search = new Search();

	public String undoTask() {
		String feedback = "";
		UndoRedoDetails detail_ = new UndoRedoDetails();
		detail_ = manager.getAction("undo");

		if (detail_ == null) {
			return feedback = "Undo is not available.";
		} else {
			AvailableCommands command_ = AvailableCommands.valueOf(detail_
					.getCommand().toUpperCase());

			switch (command_) {
				case ADD :
					feedback = addRemovedTask(detail_);
					break;
				case EDIT :
					feedback = edit.editTaskByField(detail_.getId(),
							detail_.getEditingField(), detail_.getInitialValue());
					break;
				case REMOVE :
					feedback = remove.removeTask(detail_.getId());
					break;
				case INCOMPLETE :
					feedback = edit.editTaskByField(detail_.getId(), "status",
							detail_.getInitialValue());
					break;
			}

		}
		return "Undo is successful! " + feedback;
	}

	private String addRemovedTask(UndoRedoDetails detail) {
		Task tempTask = new Task();
		tempTask.setId(detail.getId());
		assert tempTask.getId() == detail.getId();
		tempTask.setTitle(detail.getTask().getTitle());
		tempTask.setDeadline(detail.getTask().getDeadline());
		tempTask.setStatus(detail.getTask().getStatus());
		tempTask.setPriority(detail.getTask().getPriority());

		Task createdTask = TaskDirector.addTask(tempTask);
		return "Task " + createdTask.getTitle() + " added.";
	}
}
